package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TdActivity

import com.bumptech.glide.RequestManager

class TdlstjAdapter : BaseLoadmoreAdapter<TdBean.Sub> {

    val manager: RequestManager
    lateinit var goodsIndex: TdBean

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) return 1
        return super.getItemViewType(position)
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 1)
            return BindingViewHoder(
                DataBindingUtil.inflate(
                    layoutInflater,
                    R.layout.layout_tdheader,
                    parent,
                    false
                )
            )
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_tdlstj_item2,
                parent,
                false
            )
        )
    }

    fun submitData(list: MutableList<TdBean.Sub?>, goodsIndex: TdBean) {
        this.goodsIndex = goodsIndex
        list.add(
            0, TdBean.Sub(
                "", 1, "", "", "", "", "", "",
                "", "", "", "", ""
            )
        )
        super.submitList(list)
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        when (bindingViewHolder.itemViewType) {
            1 -> {
                val binding = bindingViewHolder.binding as LayoutTdheaderBinding
//                binding.name ="推荐业绩${goodsIndex.direct_buy}"
                binding.subStr = "历史奖励${goodsIndex.reward_h}"
            }
            else -> {
                val binding = bindingViewHolder.binding as LayoutTdlstjItem2Binding
                val bean = getItem(position)
                manager.load(bean.avatar).error(R.mipmap.ic_launcher).into(binding.img)
                binding.bean = bean
                binding.event = this

                if (bean != null) {
                    if (position == 1) {
                        binding.llHeader.visibility = View.VISIBLE
                    } else {
                        binding.llHeader.visibility = View.GONE
                    }
                }

                binding.isShowLevel = !(bean.level_str.equals("") || bean.level_str.length == 0)

            }
        }
    }

    fun onItemClick(v: View, b: TdBean.Sub) {
        context.startActivity(Intent(context, TdActivity::class.java).apply {
            putExtra("bean", b.id)
        })
    }

    /**
     * 历史推荐
     */
    fun lstj() {

    }

}